<?php
/**
 * PHP Daycare
 * Has all of the functions for the admin panel involving adults
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */

include_once('config.php');
include_once('functions.php');

$mode = $_GET['mode'];
session_start();
// Our user must be logged in & an admin to view things
if(loggedInAsAdmin() == 1)
{
	switch($mode)
	{
		case 'add':
			if(isset($_GET['i']))
			{
				// We are inserting a new worker into the DB
				$name = mysql_escape_string($_GET['name']);
				$address = mysql_escape_string($_GET['address']);
				$phone = mysql_escape_string($_GET['phone']);
				$dln = mysql_escape_string($_GET['dln']);
				$sql = "INSERT INTO `adults` 
					(`name`, `address`, `phone`, `d_license`) 
					VALUES 
					('$name', '$address', '$phone', '$dln');";
				$r = $db->query($sql);
				if($r)
				{
					echo "<p>The new adult <i>$name</i> was added successfully.</p>";
				}
				else
				{
					echo "<p>An unknown error occurred and the adult was not added.</p>";
				}
			}
			echo "<form name='addadult'>
			<p><label>Name:<br /><input type='text' name='name' size='25' tabindex='1'></label></p>
			<p><label>Phone Number:<br /><input type='text' name='phone' size='15' tabindex='2'></label></p>
			<p><label>Address:<br /><textarea name='address' cols='45' rows='5' tabindex='3'></textarea></label></p>
			<p><label>Driver's License Number:<br /><input type='text' name='dln' size='25' tabindex='4'></label></p>
			<p><input type='button' tabindex='5' value='Add Adult' onClick=\"loadAdmininfo('adults.php', 'mode=add&i=1&name='+encodeURIComponent(document.addadult.name.value)+'&address='+encodeURIComponent(document.addadult.address.value)+'&phone='+encodeURIComponent(document.addadult.phone.value)+'&dln='+encodeURIComponent(document.addadult.dln.value))\"/></p>
			</form>";
			break;
		case 'delete':
			// We may be deleting an adult
			if(isset($_GET['id']))
			{
				$id = $_GET['id'];
				// Lets let them know exactly who they are deleting.
				$sql = "SELECT name FROM adults WHERE ID=$id";
				$r = $db->query($sql);
				$row = mysql_fetch_row($r);
				$name = $row[0];
				// Now we delete
				$sql = "DELETE FROM adults WHERE ID=$id";
				$r = $db->query($sql);
				if($r)
				{
					echo "<p>The adult <i>$name</i> was deleted.</p>";
				}
				else
				{
					echo "<p>An error occurred. <i>$name</i> was not deleted.</p>";
				}
			}
			// We need a list of all the adults.
			$sql = "SELECT ID, name FROM adults;";
			$r = $db->query($sql);
			echo "
			<p>Please select the adult you wish to delete. This operation CAN NOT be undone. Child connected with this adult will NOT be deleted.</p>
			<table cellspacing='1' class='tablesorter' width='300px'>
			<thead>
				<tr>
					<th class=\"{sorter: 'text'}\">Name</th>
					<th class=\"{sorter: 'text'}\">Delete</th>
				</tr>
			</thead>
			<tbody>";
			while($row = mysql_fetch_row($r))
			{
				echo "<tr>
							<td>" . $row[1] . "</td>
							<td><a href= \"#\" onClick=\"loadAdmininfo('adults.php', 'mode=delete&id=".$row[0]."')\">Delete</a></td>
					</tr>";
			}
			echo "</tbody></table>";
			$temp->tablePager();
			echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
			break;
		case 'modify':
			$mm =  'v';
			if(isset($_GET['mm']))
			{
				$mm = $_GET['mm'];
			}
			switch($mm)
			{
				case 'v': // Default action - view
					// We need a list of all the adults.
					$sql = "SELECT ID, name FROM adults";
					$r = $db->query($sql);
					echo "
					<p>Please select the adult you wish to modify.</p>
					<table cellspacing='1' class='tablesorter' width='300px'>
					<thead>
						<tr>
							<th class=\"{sorter: 'text'}\">Name</th>
							<th class=\"{sorter: 'text'}\">Modify</th>
						</tr>
					</thead>
					<tbody>";
					while($row = mysql_fetch_row($r))
					{
						echo "<tr>
									<td>" . $row[1] . "</td>
									<td><a href= \"#\" onClick=\"loadAdmininfo('adults.php', 'mode=modify&mm=m&id=".$row[0]."')\">Modify</a></td>
							</tr>";
					}
					echo "</tbody></table>";
					$temp->tablePager();
					echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
					break;
				case 'm': // Modifcation page for user
					$id = $_GET['id'];
					$sql = "SELECT * FROM adults WHERE ID=$id;";
					$r = $db->query($sql);
					$row = mysql_fetch_row($r);
					// Start the accordion
					echo "<p>Please select from below what you wish to modify for <i>".$row[1]."</i></p>";
					echo "<div id='accordion'>";
					 echo "<div><h4 class='ui-acc-header-back'><a href='#' class='ui-acc-header'>Modify Information</a></h4><div>";
						echo "<p>Please update the adults's information below as desired.</p>";
						echo "<form name=\"editadult\">
						<p><label>Name:<br /><input type='text' name='name' size='25' tabindex='1' value='${row['1']}'></label></p>
						<p><label>Address:<br /><textarea name='address' cols='45' rows='5' tabindex='2'>${row['2']}</textarea></label></p>
						<p><label>Telephone:<br /><input type='text' name='phone' size='25' tabindex='3' value='${row['3']}'></label></p>
						<p><label>Driver's License Number:<br /><input type='text' name='dln' size='25' tabindex='4' value='${row['4']}'></label></p>
						<p><input type='button' tabindex='5' value='Update Adult' onClick=\"loadAdmininfo('adults.php', 'mode=modify&mm=u&id=$id&name='+encodeURIComponent(document.editadult.name.value)+'&address='+encodeURIComponent(document.editadult.address.value)+'&phone='+encodeURIComponent(document.editadult.phone.value)+'&dln='+encodeURIComponent(document.editadult.dln.value))\"/></p></form>";
					 echo "</div></div>";
					 echo "<div><h4 class='ui-acc-header-back'><a href='#' class='ui-acc-header'>Manage Photo</a></h4><div>";
						echo "<p>Click the link below to open a new window which will show the current picture and allow you to upload a new picture. Please note that only JPEG (.jpeg, .jpg) images will be accepted.</p>";
						//echo "<p><a href= \"#\" onClick=\"window.open('imgupload.php?id=".$row[0]."&t=a','mywindow','width=400,height=600,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')\">Upload New Picture</a></p>";
						echo "<p><input type='button' value='View Current Photo' class='button'>&nbsp;&nbsp;<input type='button' value='Upload new Photo' onClick=\"window.open('imgupload.php?id=".$row[0]."&t=a','mywindow','width=400,height=600,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')\"></p>";
						$temp->jqPopup($row[1],"<center><img src='aimg.php?id=$id'></center>");
					 echo "</div></div>";
					 echo "<div><h4 class='ui-acc-header-back'><a href='#' class='ui-acc-header'>Manage Connections</a></h4><div>";
						// Add connected children
						$sql = "SELECT ID, Name from child";
						$r = $db->query($sql);
						$sql = "SELECT child FROM c_a_rel WHERE adults = $id";
						$c = $db->query($sql);
						$num = 0;
						$connection[$num] = "";
						while($conn = mysql_fetch_row($c))
						{
							$connection[$num] = $conn[0];
							$num++;
						}
						echo "
						<form name='childadult'>
						<p>Please select any children you wish this adult to be connected to.</p>
						<div class='childscroll'>
						<table cellspacing='1' class='tablesorter' width='200px'>
						<thead>
							<tr>
								<th class=\"{sorter: 'none'}\" width=\"10px\"></th>
								<th class=\"{sorter: 'text'}\">Name</th>
							</tr>
						</thead>
						<tbody>";
						while($row = mysql_fetch_row($r))
						{
							$check = "";
							if(in_array($row[0], $connection))
							{
								$check = " checked";
							}
							echo "<tr>
										<td><input type='checkbox' name='checkgroup' value='".$row[0]."'$check/></td>
										<td>".$row[1]."</td>
									</tr>";
						}
						echo "</tbody></table></div>
						<input type='button' tabindex='5' value='Update Connections' onClick=\"loadAdmininfo('adults.php', 'mode=modify&mm=con&aid=$id' + checkboxSubmit('childadult'))\" />
						</form>";
					 echo "</div></div></div><p></p>";
					/*
					* @todo Repair the problem that probably involves JS where I can't sort while in the accordian
					*/
					//echo "<script type=\"text/javascript\">loadsorter()</script>";
					echo "<script type='text/javascript'>loadPopupCalls()</script>";
					echo "<script type=\"text/javascript\">loadAccordion()</script>";
					break;
				case 'u': // Update the modifications then set page to view workers.
					// We are update a worker in the DB
					$id = mysql_escape_string($_GET['id']);
					$name = mysql_escape_string($_GET['name']);
					$address = mysql_escape_string($_GET['address']);
					$phone = mysql_escape_string($_GET['phone']);
					$dln = mysql_escape_string($_GET['dln']);
					$sql = "UPDATE `adults` SET `name` = '$name', `address` = '$address', `phone` = '$phone', `d_license` = '$dln' WHERE `ID` = '$id';";
					$r = $db->query($sql);
					if($r)
					{
						echo "<p>The adult <i>$name</i> was modified successfully.</p>";
					}
					else
					{
						echo "<p>An unknown error occurred and the adult was not updated.</p>";
					}
					break;
				case 'con': // Update the connections
					$numchk = $_GET['numchk'];
					$aid = $_GET['aid'];
					$chk[0] = "";
					for($i = 0; $i < $numchk; $i++)
					{
						$chk[$i] = $_GET['chck'.$i];
					}
					// We have to remove all the current records, buts lets use a transaction
					// so that we don't ROYALLY mess things up
					$r = $db->query("BEGIN");
					$r = $db->query("DELETE FROM c_a_rel WHERE adults = $aid");
					foreach($chk as $cid)
					{
						$sql = "INSERT INTO c_a_rel (`child`, `adults`) VALUES ($cid, $aid)";
						$r = $db->query($sql);
						if(!$r)
						{
							$r = $db->query("ROLLBACK");
							echo "<p>An error occured and the connections where not updated.</p>";
							break;
						}
					}
					$r = $db->query("COMMIT");
					echo "<p>The connections have been updated</p>";
					break;
			}
			break;
		case 'photo':
			
				// We need a list of all the adults.
				$sql = "SELECT ID, name FROM adults;";
				$r = $db->query($sql);
				echo "
				<p>Please select the adult you wish to upload a picture for. A new window will open to allow uploading.</p>
				<table cellspacing='1' class='tablesorter' width='300px'>
				<thead>
					<tr>
						<th class=\"{sorter: 'text'}\">Name</th>
						<th class=\"{sorter: 'text'}\">Upload</th>
					</tr>
				</thead>
				<tbody>";
				while($row = mysql_fetch_row($r))
				{
					echo "<tr>
								<td>" . $row[1] . "</td>
								<td><a href= \"#\" onClick=\"window.open('imgupload.php?id=".$row[0]."&t=a','mywindow','width=400,height=600,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')\">Upload</a></td>
						</tr>";
				}
				echo "</tbody></table>";
				$temp->tablePager();
				echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
			break;
	}
}
else
{
	// not logged in or not admin
	echo "<p>Error: You are not logged in, or are not an administrator. 
	Please log in as an administrator if you wish to access this area. 
	You may login <a href='index.php'>here</a>.</p>";
}
?>
